<?php

/**
 * 后台系统管理 - 控制器类
 * 
 * @package    01CMS
 * @subpackage admin
 * @author     rolong at vip.qq.com
 * @version    1.0.0
 * @link       http://www.01cms.com
 */

class AdminController extends Controller
{
    public $title = '后台系统管理';
    public $User;

    function __construct ()
    {
        parent::__construct();
        
        if ($this->m != 'login')
        {
            $this->User = model('User');
            
            if (! $this->User->isLogined())
            {
                redirect('admin/login');
            }
        }
        
        if (! in_array($this->m, array('login', 'logout', 'index', 'top', 'menu', 'body')))
        {
            if (! $this->User->checkAuth())
            {
                showError('您的权限不足');
            }
        }
    }

    function index ()
    {
        $this->Load->view('smarty');
    }

    function login ($action = '')
    {
        if ($action == 'check')
        {
            $data = $this->Load->post('validateCode|验证码不能为空,username|用户名不能为空,password|密码不能为空');
            
            if ($error = $this->Load->getError())
            {
                response($error);
            }
            
            if (! Session::checkCode('validateCode', $data['validateCode']))
            {
                response('验证码不正确');
            }
            
            if (model('User')->login($data))
            {
                response('登陆成功', TRUE);
            }
            else
            {
                response(model('User')->getError());
            
            }
        }
        else
        {
            $url = $this->Load->get('url', 'one');
            $this->url = empty($url) ? BOOT_URL . '/admin/index' : $url;
            
            $this->Load->view('smarty');
        }
    }

    function logout ()
    {
        $this->User->logout();
        redirect('admin/login');
    }

    function help ()
    {
        echo '建设中...';
    }

    function menu ($action = '', $id = 0)
    {
        if (empty($action))
        {
            $this->menu = $this->User->getMenu();
            $this->Load->view('smarty', 'list');
        }
        else if ($action == 'reload')
        {
            $this->Load->view('smarty', 'reload');
        }
        else if ($action == 'order')
        {
            header('Cache-Control:no-cache, must-revalidate');
            $this->table = '@@__admin_menu';
            model()->ordering($id);
        }
        else
        {
            $this->title .= ' => 菜单管理';
            
            if ($action == 'update' || $action == 'insert')
            {
                $this->data = $this->Load->post('title|模块名称不能为空,items|菜单内容为能为空,hidden');
                
                if ($error = $this->Load->getError())
                {
                    response($error);
                }
            }
            
            $this->id = $id;
            $this->order = 'ordering asc';
            $this->action($action);
        }
    }

    function top ()
    {
        $this->Load->view('smarty');
    }

    function body ()
    {
        $this->channel = $this->Load->model("Channel")->getData();
        
        $this->Load->view('smarty');
    }

    function user ($action = 0, $id = '')
    {
        $this->title .= ' => 用户管理';
        
        switch ($action)
        {
            case 'lists':
            {
                $groups = $this->User->getGroups();
                
                foreach ($groups as $group)
                {
                    $groupsArr[$group->id] = $group->name;
                }
                
                $this->groups = $groupsArr;
                
                break;
            }
            case 'add':
            {
                $this->groups = $this->User->getGroups();
                break;
            }
            case 'insert':
            {
                $data = $this->Load->post('username|用户名不能为空,password|密码不能为空,groupId|请选择用户组');
                
                if ($error = $this->Load->getError())
                {
                    response($error);
                }
                
                $this->User->addUser($data);
                
                if ($error = $this->User->getError())
                {
                    response($error);
                }
                else
                {
                    response('添加成功', TRUE);
                }
                
                break;
            }
            case 'mod':
            {
                $this->groups = $this->User->getGroups();
                break;
            }
            case 'update':
            {
                $data = $this->Load->post("username|用户名不能为空,groupId|请选择用户组|password");
                
                if ($error = $this->Load->getError())
                {
                    response($error);
                }
                
                $data['id'] = $id;
                $this->User->modUser($data);
                
                if ($error = $this->User->getError())
                {
                    response($error);
                }
                else
                {
                    response('修改成功', TRUE);
                }
                
                $this->data = $data;
                
                break;
            }
            //No default
        }
        
        $this->id = $id;
        $this->action($action);
    }

    function group ($action = 0, $id = '')
    {
        $this->title .= ' => 组别管理';
        
        $id = ereg_replace('[^0-9,]', '', $id);
        
        if ($action == 'update' || $action == 'insert')
        {
            
            $name = $this->Load->post('name|组别名称不能为空');
            
            if ($error = $this->Load->getError())
            {
                response($error);
            }
            
            $auth = $this->Load->post('auth|您还没有选择权限');
            
            if ($error = $this->Load->getError())
            {
                response($error);
            }
            $this->data['name'] = $name['name'];
            $this->data['auth'] = implode(',,', $auth['auth']);
        }
        
        switch ($action)
        {
            case 'add':
            {
                $this->auth = $this->User->getAuths();
                break;
            }
            case 'insert':
            {
                $this->User->addGroup($this->data);
                
                if ($error = $this->User->getError())
                {
                    response($error);
                }
                else
                {
                    response('添加成功', TRUE);
                }
                
                break;
            }
            case 'mod':
            {
                $this->auth = $this->User->getAuths();
                $this->group = $this->User->getGroup("id = {$id}");
                $groupAuth = explode(',,', $this->group->auth);
                $count = count($this->auth);
                for ($i = 0; $i < $count; $i ++)
                {
                    if (in_array($this->auth[$i]->auth, $groupAuth))
                    {
                        $this->auth[$i]->checked = 'checked="checked"';
                    }
                    else
                    {
                        $this->auth[$i]->checked = '';
                    }
                }
                break;
            }
            case 'update':
            {
                
                $this->data['id'] = $id;
                $this->User->modGroup($this->data);
                
                if ($error = $this->User->getError())
                {
                    response($error);
                }
                else
                {
                    response('修改成功', TRUE);
                }
                
                break;
            }
            //No default
        }
        
        $this->id = $id;
        $this->action($action);
    }

    function auth ($action = '', $id = 0)
    {
        $this->title .= ' => 权限管理';
        if ($action == 'update' || $action == 'insert')
        {
            $this->data = $this->Load->post('ordering,line,name|名称不能为空,auth|权限内容不能为空');
            
            if ($error = $this->Load->getError())
            {
                response($error);
            }
        }
        
        $this->id = $id;
        $this->order = 'ordering asc';
        $this->action($action);
    }
}